﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Web_cartfirst : System.Web.UI.Page
{
    #region Variables
    DataTable cartproduct;
    #endregion


    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Session["key"] = "~/img/Process/2.png";

            if((Session["cartview"]==null) && (Session["cart"]==null))
            {
                BindGridView();
            }
            if (Session["cartview"] != null)
            {
                cartproduct = (DataTable)Session["cartview"];
                
            }
            else
            {

                cartproduct = new DataTable();
                InitializeDataSource();
            }

            #region Loading Sidebar Menu
            if (!this.IsPostBack)
            {
                ASPxButton2.Visible = false;
                ASPxButton1.Visible = false;
                List<Bestlife.ERP.Entities.E_Cart_Catogory> cat = Bestlife.ERP.DataAccess.D_Cart_Catogory.CatogoryNames();
                Bestlife.ERP.Entities.E_Cart_Catogory caten = new Bestlife.ERP.Entities.E_Cart_Catogory();
                foreach (Bestlife.ERP.Entities.E_Cart_Catogory rat in cat)
                {
                    ASPxMenu1.Items.Add(rat.Cat_Name1, rat.Cat_Name1, imageUrl: (""), navigateUrl: ("Cart.aspx?CID=" + rat.Cat_Id1));
                }
            }
            #endregion
             
            #region Set the position to the grid in pageload
            if (IsPostBack)
            {
                
                ClientScript.RegisterStartupScript(this.GetType(), "hash", "location.hash = '#cartgrid';", true);
            }
            #endregion

            #region Disable Show Cart Button
            ASPxButton5.Visible = false;
            ASPxButton6.Visible = false;
            #endregion
        }
        catch(Exception ee) 
        {
            string msg = ee.ToString();
            string[] arr = msg.Split('.');
            string message1 = arr[1].ToString();
            Response.Redirect("404.aspx?msg='" + message1 + "'");
        }
    }

    private void BindGridView()
    {
        try
        {
            if (Session["cartview"] != null)
            {

                // Get the DataTable from ViewState.
                DataTable cartAdd = (DataTable)Session["cartview"];

                // Co nvert the DataTable to DataView.
                DataView cartvb = new DataView(cartAdd);

                // Bind the GridView control.
                GridView1.DataSource = cartvb;
                GridView1.DataBind();
            }
        }
        catch(Exception ee)
        {
            string msg = ee.ToString();
            string[] arr = msg.Split('.');
            string message1 = arr[1].ToString();
            Response.Redirect("404.aspx?msg='" + message1 + "'");
        }
    }

    private void InitializeDataSource()
    {
        try
        {
            // Add four columns to the DataTable.
            cartproduct.Columns.Add("ID");
            cartproduct.Columns.Add("Code");
            cartproduct.Columns.Add("Name");
            cartproduct.Columns.Add("Price");
            cartproduct.Columns.Add("R_Price");
            cartproduct.Columns.Add("PV");
            cartproduct.Columns.Add("BV");
            cartproduct.Columns.Add("Quantity");


            // Set Produt_ID n Product_code column as the primary key.
            DataColumn[] dcKey = new DataColumn[1];
            //DataColumn[] dcKey2 = new DataColumn[1];
            dcKey[0] = cartproduct.Columns["ID"];
            cartproduct.PrimaryKey = dcKey;
        }
        catch(Exception ee) 
        {
            string msg = ee.ToString();
            string[] arr = msg.Split('.');
            string message1 = arr[1].ToString();
            Response.Redirect("404.aspx?msg='" + message1 + "'");
        }

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            ASPxButton1.Visible = true;
            ASPxButton2.Visible = true;
            Label6.Text = "Click to add items in your Cart.";
            bool bb = false;
            //
            // Split string on spaces.
            // ... This will separate all the words.
            //

            string v = Request.QueryString["GridPID"];
            string[] Id = v.Split(',');

            Label1.Text = Id[0].ToString();
            Label2.Text = Id[1].ToString();

            Bestlife.ERP.Entities.ProductE Re_Product = Bestlife.ERP.DataAccess.ProductDA.GetProuct(Label1.Text, Label2.Text);

        #region Insert To Cart

       
           
                int rowc=cartproduct.Rows.Count;
                DataView dv1 =new DataView(cartproduct);

                foreach (DataRowView drv in dv1)
                {

                    string dr = drv.Row["ID"].ToString();
                    //Label4.Text = dr;
                    if (dr.Equals(Re_Product.Product_ID))
                    {
                        Label4.Text = "Please use the edit button to update the quantity";
                        bb = true;
                    }

                }

            if(!bb){
                 string Quntity = "1";
                    // Get the DataTable from ViewState and inserting new data to it.
                    //cartproduct = (DataTable)ViewState["cartview"];
                    // Add the new row.
                cartproduct.Rows.Add(Re_Product.Product_ID, Re_Product.Code, Re_Product.Pro_Name, Re_Product.Pro_Price,Re_Product.Ret_Price,Re_Product.Point_Value, Re_Product.Business_Value, Quntity);

                Session["cartview"] = cartproduct;
                GridView1.DataSource=Session["cartview"];
                GridView1.DataBind();
              
            }
                //BindGridView();
           // }
        }
        catch (Exception ee) 
        {
            string msg = ee.ToString();
            string[] arr = msg.Split('.');
            string message1 = arr[1].ToString();
            Response.Redirect("404.aspx?msg='" + message1 + "'");
        }
        #endregion

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            if (Session["cartview"] != null)
            {
                // Get the DataTable from ViewState.
                DataTable delcart = (DataTable)Session["cartview"];

                // Get the PersonID of the selected row.
                string Pro_ID = GridView1.Rows[e.RowIndex].Cells[0].Text;

                // Find the row in DateTable.
                DataRow dlecrt = delcart.Rows.Find(Pro_ID);

                // Remove the row from the DataTable.
                delcart.Rows.Remove(dlecrt);

                // Rebind the GridView control to show data after deleting.
                BindGridView();
            }
        }
        catch (Exception ee) 
        {
            string msg = ee.ToString();
            string[] arr = msg.Split('.');
            string message1 = arr[1].ToString();
            Response.Redirect("404.aspx?msg='" + message1 + "'");
        }
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            if (Session["cartview"] != null)
            {
                string tbs = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;

                DataTable bacEdit = (DataTable)Session["cartview"];
                // Get the PersonID of the selected row.
                string Pro_ID = GridView1.Rows[e.RowIndex].Cells[0].Text;
                //string Pro_Code = GridView1.Rows[e.RowIndex].Cells[2].Text;

                // Find the row in DateTable.
                DataRow bacEditrw = bacEdit.Rows.Find(Pro_ID);

                string tbl = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;

                bacEditrw["Quantity"] = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;

                string tb = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;

                // Exit edit mode.
                GridView1.EditIndex = -1;
                //GridView1.DataBind();
                // Rebind the GridView control to show data after updating.
                BindGridView();
            }
        }
        catch (Exception ee) 
        {
            string msg = ee.ToString();
            string[] arr = msg.Split('.');
            string message1 = arr[1].ToString();
            Response.Redirect("404.aspx?msg='" + message1 + "'");
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGridView();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {

        Boolean sta = false;
        DataView dv1 = new DataView(cartproduct);
        try
        {
            if (Session["cart"] != null)
            {
                foreach (DataRowView drv in dv1)
                {

                    string Product_ID = drv.Row["ID"].ToString();
                    string Codev = drv.Row["Code"].ToString();
                    string Pro_Name = drv.Row["Name"].ToString();
                    string Pro_Price = drv.Row["Price"].ToString();
                    string R_price = drv.Row["R_Price"].ToString();
                    string Point_Value = drv.Row["PV"].ToString();
                    string Business_Value = drv.Row["BV"].ToString();
                    string Qun = drv.Row["Quantity"].ToString();


                    Bestlife.ERP.BusinessRules.Shopping_Cart cart = (Bestlife.ERP.BusinessRules.Shopping_Cart)Session["cart"];

                    string Pr_ID = Product_ID;
                    string Code = Codev;
                    string Pr_Name = Pro_Name;
                    decimal Pr_Price = decimal.Parse(Pro_Price);
                    decimal R_Price = decimal.Parse(R_price);
                    decimal Po_Value = decimal.Parse(Point_Value);
                    decimal Bu_value = decimal.Parse(Business_Value);
                    int Qun_Tb = int.Parse(Qun);

                    cart.insertItems(Pr_ID, Code, Pr_Name, Pr_Price,R_Price, Po_Value, Bu_value, Qun_Tb);
                    sta = true;
                    //DataList1.Controls[0].FindControl("Product_ID") = Re_Product.Product_ID.ToString();                 
                }

                if (sta)
                {

                }
            }
        }
        catch (Exception ee) 
        {
            string msg = ee.ToString();
            string[] arr = msg.Split('.');
            string message1 = arr[1].ToString();
            Response.Redirect("404.aspx?msg='" + message1 + "'");
        }
       
    }
    protected void ASPxButton1_Click(object sender, EventArgs e)
    {
        ASPxButton1.Visible = false;
        ASPxButton2.Visible = false;
        Label6.Text = "Click view your cart to continue.";
        BindGridView();
        //Enable Check Cart Button
        ASPxButton5.Visible = true;
        ASPxButton6.Visible = true;
        
        Boolean sta = false;
        DataView dv1 = new DataView(cartproduct);
        try
        {
            if (Session["cartview"] != null)
            {

                 Bestlife.ERP.BusinessRules.Shopping_Cart cartsession=new Bestlife.ERP.BusinessRules.Shopping_Cart();
                 Session["cart"] = cartsession;
                foreach (DataRowView drv in dv1)
                {

                    string Product_ID = drv.Row["ID"].ToString();
                    string Codev = drv.Row["Code"].ToString();
                    string Pro_Name = drv.Row["Name"].ToString();
                    string Pro_Price = drv.Row["Price"].ToString();
                    string R_price = drv.Row["R_Price"].ToString();
                    string Point_Value = drv.Row["PV"].ToString();
                    string Business_Value = drv.Row["BV"].ToString();
                    string Qun = drv.Row["Quantity"].ToString();



                    Bestlife.ERP.BusinessRules.Shopping_Cart cart = (Bestlife.ERP.BusinessRules.Shopping_Cart)Session["cart"];

                    string Pr_ID = Product_ID;
                    string Code = Codev;
                    string Pr_Name = Pro_Name;
                    
                    decimal Pr_Price = decimal.Parse(Pro_Price);
                   // decimal Pr_Price = Math.Round(Convert.ToDecimal(Pro_Price), 2);
                    decimal R_Price = decimal.Parse(R_price);
                    decimal Po_Value = decimal.Parse(Point_Value);
                    decimal Bu_value = decimal.Parse(Business_Value);
                    int Qun_Tb = int.Parse(Qun);

                    cart.insertItems(Pr_ID, Code, Pr_Name, Pr_Price, R_Price, Po_Value, Bu_value, Qun_Tb);
                    sta = true;

                    //GridView2.DataSource = Session["cart"];
                    //DataList1.Controls[0].FindControl("Product_ID") = Re_Product.Product_ID.ToString();                 
                }

                if (sta)
                {

                }
            }
        }
        catch (Exception ee) 
        {
            string msg = ee.ToString();
            string[] arr = msg.Split('.');
            string message1 = arr[1].ToString();
            Response.Redirect("404.aspx?msg='" + message1 + "'");
        }
       
    }


    protected void ASPxMenu1_ItemClick(object source, DevExpress.Web.ASPxMenu.MenuItemEventArgs e)
    {

    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindGridView();
    }
    protected void ASPxDataView3_PageIndexChanged(object sender, EventArgs e)
    {

    }
    protected void ASPxButton5_Click(object sender, EventArgs e)
    {

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void ASPxButton6_Click(object sender, EventArgs e)
    {

    }
}